package com.wgo.bpot.common.transport.util;

import java.util.logging.Logger;

/**
 * @author petterei
 * @version $Id: Assert.java 547 2008-04-29 12:53:49Z petter.eide $
 */
public class Assert {

	private static final Logger log = Logger.getLogger(Assert.class.getSimpleName());
	
	/**
	 * Used to verify that objects is not 'null'.
	 * 
	 * @param <T> The type of object parameter
	 * @param object
	 * @throws RuntimeException Throws this exception if parameter is null.
	 */
	public static <T extends Object> void assertNotNull(T object, String message) throws RuntimeException{
		if (null == object) {
			AssertionError e = new AssertionError (object); 
			throw new RuntimeException(message, e);
		}
	}

	public static <T extends Object> void assertNotNull(T object, RuntimeException throwable) throws RuntimeException{
		if (null == object) {
			AssertionError e = new AssertionError (object); 
			throwable.initCause(e);
			throw throwable;
		}
	}

	public static <T extends Object> void assertNotNull(T object) throws RuntimeException{
		assertNotNull(object, "The object was null");
	}

	public static void assertTrue(boolean expression, RuntimeException throwable) throws RuntimeException{
		if (!expression) {
			AssertionError e = new AssertionError (expression); 
			throwable.initCause(e);
			throw throwable;
		}
	}

	public static void assertTrue( boolean expression, String message ) {
		if (!expression) {
			AssertionError e = new AssertionError (expression);
			throw new RuntimeException(message, e);
		}
	}
	public static void assertTrue( boolean expression ) {
		if (!expression) {
			AssertionError e = new AssertionError (expression);
			throw new RuntimeException("Assertion failed, expression was false.", e);
		}
	}
	
	public static void assertEquals (Object o1, Object o2) {
		if ((null != o1 && o1.equals(o2) ) || (o1 == null && o2 == null )) {
			AssertionError e = new AssertionError ("unequal");
			throw new RuntimeException("Assertion failed, objects not equal", e);
		}
	}
	public static void warnIfFalse(boolean expression, String message) {
		if (!expression) {
			log.warning(message);
		}
	}
	public static void warnIfTrue(boolean expression, String message) {
		if (expression) {
			log.warning(message);
		}
	}
}
